package handler

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"starry_sky/internal/core"
	user_dto "starry_sky/internal/dto/user-dto"
	"starry_sky/internal/model"
	"starry_sky/internal/service"
	"starry_sky/internal/service/user-service"
	"starry_sky/internal/tools/encryption"
	"starry_sky/internal/tools/logger"
)

func CreateUser(c *gin.Context) {
	user := model.User{}
	err := c.ShouldBindJSON(&user)
	if logger.HasError(err) {
		c.JSON(http.StatusInternalServerError, core.NewError(core.BAD_REQUEST, "接受参数错误"))
		return
	}
	user.Creator = c.GetString("userId")
	err = user_service.CreateUser(&user)
	if logger.HasError(err) {
		c.JSON(http.StatusInternalServerError, core.NewError(core.INTERNAL_SERVER_ERROR, "新增用户失败"))
		return
	}
	c.JSON(http.StatusOK, core.OK.WithData(user.ID))
}

func UpdateUser(c *gin.Context) {
	user := model.User{}
	err := c.ShouldBindJSON(&user)
	if logger.HasError(err) {
		c.JSON(http.StatusInternalServerError, core.NewError(core.BAD_REQUEST, "接受参数错误"))
		return
	}
	user.Updater = c.GetString("userId")
	err = user_service.UpdateUser(&user)
	if logger.HasError(err) {
		c.JSON(http.StatusInternalServerError, core.NewError(core.INTERNAL_SERVER_ERROR, "更新用户失败"))
		return
	}
	c.JSON(http.StatusOK, core.OK.WithData(true))
}

func UpdateUserStatus(c *gin.Context) {
	user := model.User{}
	err := c.ShouldBindJSON(&user)
	if logger.HasError(err) || user.ID == "" {
		c.JSON(http.StatusInternalServerError, core.NewError(core.BAD_REQUEST, "接受参数错误"))
		return
	}
	updateMap := make(map[string]any)
	updateMap["status"] = user.Status
	updateMap["updater"] = c.GetString("userId")
	err = service.UpdateForMap(&user, updateMap)
	if err != nil {
		c.JSON(http.StatusInternalServerError, core.NewError(core.INTERNAL_SERVER_ERROR, "权限更新失败"))
		return
	}
	c.JSON(http.StatusOK, core.OK.WithData(true))
}

func UpdateUserPassword(c *gin.Context) {
	user := model.User{}
	err := c.ShouldBindJSON(&user)
	user.Password = encryption.MD5(user.Password)
	if user.ID == core.EmptyID || user.Password == "" {
		c.JSON(http.StatusInternalServerError, core.NewError(core.BAD_REQUEST, "接受参数错误"))
		return
	}
	err = service.UpdateForObj(&user)
	if logger.HasError(err) {
		c.JSON(http.StatusInternalServerError, core.NewError(core.INTERNAL_SERVER_ERROR, "更新用户密码失败"))
		return
	}
	c.JSON(http.StatusOK, core.OK.WithData(true))
}

func DeleteUser(c *gin.Context) {
	user := model.User{}
	err := c.ShouldBindJSON(&user)
	if logger.HasError(err) {
		c.JSON(http.StatusInternalServerError, core.NewError(core.BAD_REQUEST, "接受参数错误"))
		return
	}
	err = service.Delete(&user)
	if logger.HasError(err) {
		c.JSON(http.StatusInternalServerError, core.NewError(core.INTERNAL_SERVER_ERROR, "删除用户失败"))
		return
	}
	c.JSON(http.StatusOK, core.OK.WithData(true))
}

func PageUser(c *gin.Context) {
	support := core.PageSupport[model.User]{}
	err := c.ShouldBind(&support)
	if logger.HasError(err) {
		c.JSON(http.StatusInternalServerError, core.NewError(core.BAD_REQUEST, "接受参数错误"))
		return
	}
	err = service.FindPage(&support, map[string]any{}, make([]string, 0), "nickname")
	if logger.HasError(err) {
		c.JSON(http.StatusInternalServerError, core.NewError(core.INTERNAL_SERVER_ERROR, "查询用户失败"))
		return
	}
	c.JSON(http.StatusOK, core.OK.WithData(support))
}

func FindUserById(c *gin.Context) {
	user := model.User{}
	err := c.ShouldBind(&user)
	if logger.HasError(err) {
		c.JSON(http.StatusInternalServerError, core.NewError(core.BAD_REQUEST, "接受参数错误"))
		return
	}
	err = service.FindById(&user)
	if logger.HasError(err) {
		c.JSON(http.StatusInternalServerError, core.NewError(core.INTERNAL_SERVER_ERROR, "查询用户失败"))
		return
	}
	c.JSON(http.StatusOK, core.OK.WithData(user))
}

func GetUserInfo(c *gin.Context) {
	if value, exists := c.Get("userId"); !exists {
		c.JSON(http.StatusInternalServerError, core.NewError(core.BAD_REQUEST, "用户id不能为空"))
		return
	} else {
		id := value.(string)
		returnMap, err := user_service.GetUserInfoById(id)
		if logger.HasError(err) {
			c.JSON(http.StatusInternalServerError, core.NewError(core.INTERNAL_SERVER_ERROR, err.Error()))
			return
		}
		c.JSON(http.StatusOK, core.OK.WithData(returnMap))
	}
}

func BindUserAndRole(c *gin.Context) {
	var bindUserAndRole user_dto.BindUserAndRole
	err := c.ShouldBindJSON(&bindUserAndRole)
	if logger.HasError(err) || bindUserAndRole.UserId == "" || len(bindUserAndRole.RoleIds) == 0 {
		c.JSON(http.StatusInternalServerError, core.NewError(core.BAD_REQUEST, "接受参数错误"))
		return
	}
	err = user_service.BindUserAndRole(bindUserAndRole)
	if logger.HasError(err) {
		c.JSON(http.StatusInternalServerError, core.NewError(core.INTERNAL_SERVER_ERROR, err.Error()))
		return
	}
	c.JSON(http.StatusOK, core.OK.WithData(true))
}
